package com.nhjf.common.util.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * shopping工具类
 * @author hjy
 *
 */
public class ShoppingTools {
	
	//生产订单编号
	public static String selectOrderNo(String orderType,String identityNum){
	  String orderNo="";
	  String type="";
	  String oldDate="";
	  String seqNo="";
	  SimpleDateFormat fm=new SimpleDateFormat("yyyyMMdd");
	  String nowdate=new String(fm.format(new Date()));
	  
		if(orderType.equals("1")){
			type="1";
			oldDate=identityNum.substring(1,9);
			seqNo=identityNum.substring(9);
		}else if(orderType.equals("2")){
			type="02";
			oldDate=identityNum.substring(2,10);
			seqNo=identityNum.substring(10);
		}else if(orderType.equals("3")){
			type="3";
			oldDate=identityNum.substring(1,9);
			seqNo=identityNum.substring(9);
		}
       //1201105240001
       if(!nowdate.equals(oldDate)){//判断是否当天
    	   oldDate=nowdate;
       }
       
       Integer no=Integer.parseInt(seqNo)+1;
       String orderseqno=String.valueOf(no);
       if(orderseqno.length()==1){
    	   orderNo="000"+orderseqno;
       }if(orderseqno.length()==2){
    	   orderNo="00"+orderseqno;
       }if(orderseqno.length()==3){
    	   orderNo="0"+orderseqno;
       }
       if(orderseqno.length()>4){
    	   
       }
		orderNo=type+oldDate+orderNo;
		return orderNo;
	}
	
	
	//生成订单编号 规则 6位日期+6位流水号+6位商户编号
	public static String getOrderNo(String mercId,String orderGlide){
		String glide = "",orderNo = "",mercNo = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
		Date date=new Date();
		String dateStr=sdf.format(date);
		//商户编号，长度6位，不足补0否则截取
		if(mercId.length() <= 6){
			mercNo = mercId;
			for(int i=0;i<6-mercId.length();i++){
				mercNo = "0" + mercNo; 
			}
		}else{
			mercNo = mercId.substring(0,6);
		}
		//流水号，长度6位，不足补0
		if(orderGlide.length() <= 6){
			glide = orderGlide;
			for(int i=0;i<6-orderGlide.length();i++){
				glide = "0" + glide; 
			}
		}
		orderNo = dateStr + glide + mercNo;
		return orderNo;
	}
	
	
	
	//生成订单编号 规则 6位日期+6位流水号
	public static String getOrderNo(String orderGlide){
		String glide = "",orderNo = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
		Date date=new Date();
		String dateStr=sdf.format(date);
		//流水号，长度6位，不足补0
		if(orderGlide.length() <= 6){
			glide = orderGlide;
			for(int i=0;i<6-orderGlide.length();i++){
				glide = "0" + glide; 
			}
		}
		orderNo = dateStr + glide;
		return orderNo;
	}
	
	

	
	
	
	//生成6位随机数  zhuheng
	public static String getSixRandom(){
		Random random = new Random();
        String result="";

        for(int i=0;i<6;i++){
            result+=random.nextInt(10);    
        }
        return result;
	}
	
	
	
	//生成8位随机数  zhuheng
	public static String getEightRandom(){
		Random random = new Random();
        String result="";

        for(int i=0;i<8;i++){
            result+=random.nextInt(10);    
        }
        return result;
	}
	
	/**
	 * 组建相应规则orderNo
	 * @param orderType 1普通订单2团购订单标识3秒杀订单标识4活动订单
	 * @param orderGlide 相应的数据库序列
	 * @return
	 */
	public String getOrderId(String orderType,String  orderGlide){
		String glide = "",orderNo = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date date=new Date();
		String dateStr=sdf.format(date);
		//流水号，长度8位，不足补0
		if(orderGlide.length() < 8){
			for(int i=0;i<8-orderGlide.length();i++){
				glide = "0" + glide; 
			}
		}
		orderNo =orderType+dateStr + glide+orderGlide;
		return orderNo;
		
	}
	

	
	
	
	
	
	
	public static void main(String[] args) {
   ShoppingTools st=new ShoppingTools();
		   String no=st.selectOrderNo("1", "01201106220001");
		    
   String s="01201106220001";
   String s1=s.substring(2,10);
   String ss=s.substring(13,14);
   String s4=s.substring(11,14);
   Integer ii = Integer.parseInt(s4)+1;
 
   Integer s2=Integer.parseInt(s4)+1;
   String s3=s2+"";
   if(s3.length()==1){
	   s3="000"+s2;
   }if(s3.length()==2){
	   s3="00"+s2;
   }if(s3.length()==3){
	   s3="0"+s2;
   }
   if(s3.length()>4){
	   
   }
 
//
	}
	
	
	
	
	/*获得当前时间*/  
    public static String getCurrentTime(){  
        Date currentTime = new Date();  
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String dateString = formatter.format(currentTime);  
        return dateString;  
    }
    
    
    
    /*时间比大小*/  
    public static int timeCompare(String t1,String t2){  
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        Calendar c1=Calendar.getInstance();  
        Calendar c2=Calendar.getInstance();  
        try {  
            c1.setTime(formatter.parse(t1));  
            c2.setTime(formatter.parse(t2));  
        } catch (ParseException e) {  
        	LogMessage.logwrite(ShoppingTools.class, e.getMessage());  
        }  
        int result=c1.compareTo(c2);  
        return result;  
    }  
    
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
